package javasec.seed.seeds;


import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.Timestamp;

import java.lang.reflect.Method;
import java.lang.Thread;

import java.util.Properties;

import javasec.seed.Exposed;
import javasec.seed.Seed;
import javasec.seed.SeedServer;


public class MACNetworkMapper extends Seed
{
	//private Connection conn=SeedServer.conn;
	//private boolean mapping=false;

	public MACNetworkMapper() throws RemoteException
	{
		/*
		System.out.println("MACNetworkMapper: starting up");
		try
		{
			Statement statement = conn.createStatement();

			statement.execute(
				"create table BLUESEED_MAC_NODES("+
					"MAC varchar(40) not null unique,"+
					"MAC_ID integer not null primary key generated always as identity,"+
					"MAC_PARENT varchar(40) not null,"+
					"MAC_PARENT_ID integer not null,"+
					"NOTE varchar(100) not null,"+ // just useful to have around
					"TIMESTAMP timestamp)");

			System.out.println("MACNetworkMapper: created tables");
		}
		catch(SQLException sqle)
		{
			System.out.println("MACNetworkMapper: tables already in place");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		*/
	}


	public void addMACEdge(String mac1, String mac2)
	{
		/*
		Statement query = null;
		PreparedStatement preparedQuery = null;
		ResultSet resultSet = null;
		
		int gatewayID=-2;

		try // get the gatewayID, insert a node for the gateway if needed
		{
			preparedQuery=conn.prepareStatement("select * from BLUESEED_MAC_NODES where MAC=?");
			preparedQuery.setString(1,mac2);
			resultSet=preparedQuery.executeQuery();
			
			if(resultSet.next()) // the gateway is known
			{
				// need to get gateway ID
				gatewayID=resultSet.getInt(2);
			}
			else // this gateway is not known
			{
				System.out.println("MACNetworkMapper: adding new gateway "+mac2);
				preparedQuery=conn.prepareStatement(
					"insert into BLUESEED_MAC_NODES"+
					"(MAC,MAC_PARENT,MAC_PARENT_ID,NOTE,TIMESTAMP) values (?,?,?,?,?)");
				
				preparedQuery.setString(1,mac2);
				preparedQuery.setString(2,"internet");
				preparedQuery.setInt(3,-1);
				preparedQuery.setString(4,"");
				preparedQuery.setTimestamp(5,new Timestamp(System.currentTimeMillis()));
				
				preparedQuery.executeUpdate();
				conn.commit();
				
				// now get the parent
				preparedQuery=conn.prepareStatement("select * from BLUESEED_MAC_NODES where MAC=?");
				preparedQuery.setString(1,mac2);
				resultSet=preparedQuery.executeQuery();
				resultSet.next();
				gatewayID=resultSet.getInt(2);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		// see if the node is already in the database
		try
		{
			preparedQuery=conn.prepareStatement("select * from BLUESEED_MAC_NODES where MAC=?");
			preparedQuery.setString(1,mac1);
			resultSet=preparedQuery.executeQuery();
			if(resultSet.next()) // return if node exists already
			{
				// see if the gateway is still accurate
				//String parentMac=resultSet. GETIT()
				if(parentMac.equals())

				return;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		// insert the new node
		try
		{
			System.out.println("MACNetworkMapper: adding new node "+mac1);
			preparedQuery=conn.prepareStatement(
				"insert into BLUESEED_MAC_NODES"+
				"(MAC,MAC_PARENT,MAC_PARENT_ID,NOTE,TIMESTAMP) values (?,?,?,?,?)");
			
			preparedQuery.setString(1,mac1);
			preparedQuery.setString(2,mac2);
			preparedQuery.setInt(3,gatewayID);
			preparedQuery.setString(4,"");
			preparedQuery.setTimestamp(5,new Timestamp(System.currentTimeMillis()));
			
			preparedQuery.executeUpdate();
			// TODO is the row ID returned the same as the primary key?			
		}
		catch(Exception e)
		{
			//e.printStackTrace();
		}

		*/
	}

}

